package chapter1;

import lombok.extern.slf4j.Slf4j;

/**
 * @author caiya
 * @date 2020/3/12 20:45
 */
@Slf4j
public class InterruptedExceptTest {
    static Object obj = new Object();

    public static void main(String[] args) throws InterruptedException {
        /**
         * 演示当一个线程A调用共享对象的wait方法被阻塞挂起之后，如果这时候其他线程B中断了该线程，
         * 那么线程A会抛出InterruptedException，也是在wait方法处抛出中断异常
         *
         * threadA阻塞挂起的时候，线程B调用threadA的interrupt方法中断线程A，那么A线程会直接跑出InterruptedException
         */

        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                log.debug("------begin------");
                synchronized (obj) {
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        threadA.start();
        Thread.sleep(1000);

        threadA.interrupt();
    }
}
